package com.wqh.zeromq.demo;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

public class HWCommonClient {

    public static void main(String[] args) {
        //实例化socket对象
        Socket sockect = new Socket();
        //用socket连接服务器
        try {
            sockect.connect(new InetSocketAddress("localhost",5555));
            //由socket获取输入输出流
            InputStream in = sockect.getInputStream();
            OutputStream out = sockect.getOutputStream();

            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(System.in));
            String line;
            while((line = reader.readLine())!=null){
                //向服务器发送数据
                out.write(line.getBytes());
                System.out.println(">>"+line);

                //接收数据,循环将数据写入byte[]中
                byte[] buff = new byte[128];
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                int num =0;
                while((num = in.read(buff))>0){
                    bos.write(buff,0 ,num);
                }
                System.out.println(">>"+new String(bos.toByteArray()));
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
